import { wechatrequest } from './API/request'
import { getStorageSync } from '../crossplatform/apiservice/storage'

/**
 * 切换用户身份
 */
export const changeIdentityRequest = ({ id, role }) => {
  return wechatrequest.put('/userInfo/changeIdentity', {
    data: { id, role }
  })
}

/**
 * 创建职初C用户
 */
export const createAccountRequest = ({
  birth,
  census = '',
  mobile,
  residence,
  role,
  userAvator,
  userId,
  userName,
  sex
}) => {
  let params = {}
  if (userAvator) {
    params['userAvator'] = userAvator
  }
  birth = birth + (birth.length === 4 ? '-01-01 00:00:00' : '')
  return wechatrequest.postparams('/userInfo/createAccount', {
    data: { sex, birth, census, mobile, residence, role, userId, userName, ...params }
  })
}

/**
 * 编辑用户信息
 */
export const editUserInfoRequest = ({
  id,
  census = '',
  sex,
  gmtBirth,
  mobile,
  residence,
  userAvator,
  userName
}) => {
  let params = {}
  if (census) {
    params['census'] = census
  }
  if (gmtBirth) {
    params['gmtBirth'] = gmtBirth
  }
  if (mobile) {
    params['mobile'] = mobile
  }
  if (residence) {
    params['residence'] = residence
  }
  if (userAvator) {
    params['userAvator'] = userAvator
  }
  if (userName) {
    params['userName'] = userName
  }

  if (sex) {
    params['sex'] = sex
  }
  const userinfo = getStorageSync('userinfo')
  const userId = userinfo.userId
  return wechatrequest.put('/userInfo/editUserInfo', {
    data: {
      id,
      userId,
      ...params
    }
  })
}

/**
 * 编辑自我评价
 */
export const editSelfEvaluation = ({ selfEvaluation, archiveId }) => {
  const userinfo = getStorageSync('userinfo')
  const userId = userinfo.userId
  return wechatrequest.put(`/userArchive/updateSelfEvaluation`, {
    data: {
      userId,
      selfEvaluation,
      archiveId
    }
  })
}

/**
 * 创建职初B用户
 */
export const createAccountForBRequest = ({
  mobile,
  position,
  userAvator,
  userId,
  userName,
  sex,
  birthday
}) => {
  let params = {}
  if (userAvator) {
    params['userAvator'] = userAvator
  }
  const userinfo = getStorageSync('userinfo')
  const openid = userinfo.openId
  return wechatrequest.postparams('/userInfo/createAccountForB', {
    data: { mobile, position, userId, userName, sex: sex == 2 ? 'female' : 'male', birthday: birthday + (birthday.length === 4 ? '-01-01 00:00:00' : ''), ...params, openid }
  })
}

/**
 * 编辑职初B用户
 */
export const editAccountForBRequest = ({
  userId,
  id,
  mobile,
  position,
  userAvator,
  userName,
  sex,
  birthday
}) => {
  let params = {}
  if (userAvator) {
    params['userAvator'] = userAvator
  }
  return wechatrequest.postparams('/userInfo/editInfoForB', {
    data: {
      userId,
      id,
      mobile,
      position,
      userName,
      sex,
      birthday:  birthday + (birthday.length === 4 ? '-01-01 00:00:00' : ''),
      ...params
    }
  })
}

/**
 * 首页简历基本信息
 */
export const getArchiveInfoRequest = ({ userId = '' }) => {
  const userinfo = getStorageSync('userinfo') || ''
  return wechatrequest.get('/userInfo/getArchiveInfo', {
    data: {
      id: userId || userinfo.userId || ''
    }
  })
}

/**
 * 根据auth userid拿职初用户信息
 */
export const getZhichuUserInfoByAuth = ({ userId }) => {
  return wechatrequest.get(`/userInfo/getUserInfoByUserId`, {
    data: {
      userId
    }
  })
}

/**
 * 根据auth userid拿职初b用户信息
 */
export const getZhichuUserInfoBByAuth = ({ userId }) => {
  return wechatrequest.get('/userInfo/getUserInfoForB', {
    data: {
      userId
    }
  })
}

/**
 * 根据切换用户身份
 */
export const changeToRole = ({ role, userId }) => {
  let url = ''
  if (role == 'personal') {
    url = '/userInfo/changeToB'
  } else {
    url = '/userInfo/changeToC'
  }
  return wechatrequest.put(url, {
    data: {
      userId
    }
  })
}

/**
 * 修改手机号
 */
export const changeMobileRequest = ({ identifier, type, mobile, code }) => {
  return wechatrequest.post(`@@/auth/mobile/change`, {
    data: { identifier, type, mobile, code }
  })
}
